class ActivitiesController < ApplicationController

  before_filter :find_activity,:only=>[:add_collection,:show]
  before_filter :find_project,:only=>[:add_collection,:show]
  before_filter :find_tasks,:only=>[:add_collection,:show]
  before_filter :find_collections,:only=>[:add_collection,:show]

  def add_collection
    @collection = Collection.new(params[:collection].merge(:activity_id=>@activity.id,
        :collected_by=>current_user.id))

    if params[:collection][:collection_date].empty?
      @collection.collection_date = Date.today
    end

    @display_form="block"

    if @activity.collection_percent_total >= 100
      flash[:error] = "Total percent for phase is already 100%"
      render :action=>:show and return
    end

    if @collection.percent.nil?
      flash[:error] = "Phase percent for money collection cannot be zero!"
      render :action=>:show and return
    end

    if @collection.percent!=nil and (@activity.collection_percent_total + @collection.percent) > 100
      flash[:error] = "Total percent for phase must not exceed 100%!"
      render :action=>:show and return
    end

    if @collection.percent.to_i.==0
      flash[:error] = "Phase percent for collection cannot be zero!"
      render :action=>:show and return
    end

    flash.clear

    if @collection.save
      flash[:notice] = 'New money collection added.'
      redirect_to project_activity_url(@activity.project_id,@activity)
    else
      render :action=>:show and return
    end

  end

  # GET /activities
  # GET /activities.xml
  def index
    @activities = Activity.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @activities }
    end
  end

  # GET /activities/1
  # GET /activities/1.xml
  def show
    
    @collection = Collection.new()
    @display_form="none"
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @activity }

    end
   
  end

  # GET /activities/new
  # GET /activities/new.xml
  def new
    @activity = Activity.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @activity }
    end
  end

  # GET /activities/1/edit
  def edit
    @activity = Activity.find(params[:id])
  end

  # POST /activities
  # POST /activities.xml
  def create
    @activity = Activity.new(params[:activity])

    respond_to do |format|
      if @activity.save
        flash[:notice] = 'Activity was successfully created.'
        format.html { redirect_to(@activity) }
        format.xml  { render :xml => @activity, :status => :created, :location => @activity }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @activity.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /activities/1
  # PUT /activities/1.xml
  def update
    @activity = Activity.find(params[:id])

    respond_to do |format|
      if @activity.update_attributes(params[:activity])
        flash[:notice] = 'Activity was successfully updated.'
        format.html { redirect_to(@activity) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @activity.errors, :status => :unprocessable_entity }
      end
    end
  end

  def destroy_collection
    @collection = Collection.find params[:id]
    @activity = @collection.activity
    if @collection.destroy
      flash[:notice]="Money collection deleted."
    end
    redirect_to project_activity_url(@activity.project_id,@activity)
  end

  # DELETE /activities/1
  # DELETE /activities/1.xml
  def destroy
    @activity = Activity.find(params[:id])
    @activity.destroy

    respond_to do |format|
      format.html { redirect_to(activities_url) }
      format.xml  { head :ok }
    end
  end
end

private

def find_collections
  @collections = Collection.find :all,:conditions=>"activity_id=#{@activity.id}",:order=>'collection_date DESC'
end

def find_activity
  if params[:activity] and params[:activity][:id]!=nil
    id = params[:activity][:id]
    @activity = Activity.find id
  elsif params[:id]!=nil
    id =  params[:id]
    @activity = Activity.find id
  end
  
end

def find_project
  @project = @activity.project
end

def find_tasks
  @tasks_waiting_for_approval = Task.find_all_by_activity_id @activity.id,:conditions=>'status="Waiting for Approval"'
  @tasks = Task.find_all_by_activity_id @activity.id,:conditions=>'status<>"Waiting for Approval"'
end
